package contactbook.service.impl;

import java.util.List;
import java.util.Set;

import javax.annotation.EJB;

import javax.ejb.Remote;
import javax.ejb.Stateless;

import contactbook.dao.AccountDAOInterface;
import contactbook.dao.ContactDAOInterface;
import contactbook.dao.GroupDAOInterface;
import contactbook.model.Account;
import contactbook.model.Contact;
import contactbook.model.Group;
import contactbook.service.ContactServiceInterface;

@Stateless
@Remote(ContactServiceInterface.class)
public class ContactService implements ContactServiceInterface {

	private static final int NULL_ID = -1;
	
	private int ownerId;
	
	@EJB
	private AccountDAOInterface accountDAO;
	
	@EJB
	private GroupDAOInterface groupDAO;
	
	@EJB
	private ContactDAOInterface contactDAO;
	
	public ContactService() {
		logout();
	}
	
	@Override
	public void login(String login, String password) {
		Account ownerAccount = accountDAO.findByLoginPassword(login, password);
		ownerId = (ownerAccount != null) ? ownerAccount.getId() : NULL_ID;
	}

	@Override
	public void logout() {
		ownerId = NULL_ID;
	}
	
	/* Adds a contact and affect it to the group with the id 'groupId' */
	@Override
	public Contact add(Contact contact, int groupId) {
		if (isLogged()) {
			Group group = groupDAO.findById(groupId);
			if (group.getAccount().getId() == ownerId) {
				Account a = accountDAO.findById(ownerId);
				Contact c = a.addContact(contact);
				c = group.addContact(contact);
				c = contactDAO.addContact(contact);
				return c;
			}
		}
		return null;
	}
	
	/* Adds a contact without affecting to any group */
	@Override
	public Contact add(Contact contact) {
		if (isLogged()) {
			Account a = accountDAO.findById(ownerId);
			Contact c = a.addContact(contact);
			c = contactDAO.addContact(contact);
			return c;
		}
		return null;
	}
	
	@Override
	public void update(Contact contact) {
		if (isLogged())
			if (contact.getAccount().getId() == ownerId)
				contactDAO.update(contact);
	}

	@Override
	public void delete(int contactId) {
		if (isLogged()) {
			Contact c = contactDAO.findById(contactId);
			if (c != null)
				if (ownerId == c.getAccount().getId())
					contactDAO.delete(contactId);
		}
	}

	@Override
	public Contact getContactById(int contactId) {
		return (isLogged()) ? contactDAO.findById(contactId) : null;
	}
	
	@Override
	public List<Contact> getContactsByName(String firstName, String lastName) {
		return (isLogged()) ? contactDAO.findByName(ownerId, firstName, lastName) : null;
	}
	
	@Override
	public Set<Contact> getContacts() {
		if (isLogged()) {
			Account account = accountDAO.findById(ownerId);
			return account.getContacts();
		}
		return null;
	}
	
	@Override
	public Set<Contact> getContacts(int groupId) {
		if (isLogged()) {
			Group group = groupDAO.findById(groupId);
			return group.getContacts();
		}
		return null;
	}
	
	@Override
	public List<Contact> getContactsWithoutGroup() {
		return (isLogged()) ? contactDAO.getContactsWithoutGroup(ownerId) : null;
	}

	@Override 
	public void setDAO(ContactDAOInterface contactDao, GroupDAOInterface groupDao, AccountDAOInterface accountDao) {
		this.accountDAO = accountDao;
		this.groupDAO = groupDao;
		this.contactDAO = contactDao;
	}
	
	@Override
	public boolean isLogged() {
		return ownerId != NULL_ID;
	}
}
